//
//  LPZWYMyTestViewController.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPZWYMyTestViewController.h"
#import "LPZWYMYTestTableViewCell.h"
#import "LPZWYMyTestFinshTableViewCell.h"
#import "LPZWYMYTestModel.h"
#define cellId @"LPZWYMYTestTableViewCell"
#define finshId @"LPZWYMyTestFinshTableViewCell"
@interface LPZWYMyTestViewController ()<UITableViewDelegate,UITableViewDataSource,LPZWYMyTestFinshTableViewCellDelegate,LPZWYMYTestTableViewCellDelegate>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
@property (nonatomic , strong) NSMutableArray * dataArray;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;

@end

@implementation LPZWYMyTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"我的考试";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
       
        [weakSelf netWorking_header];
        
    }];
//    [self.refurbish footerToScollview:self.tableView Block:^{
//        weakSelf.page ++;
//        [weakSelf netWorking_footer];
//    }];
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}


#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_getIfFinishClassInfoUseWeicatAppWithsuccessBlock:^(id object) {
        NSString *result = [object objectForKey:@"code"];
        if ([result integerValue] == 1) {
            NSArray *array = [object objectForKey:@"data"];
            if (array.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                
            }else{
                NSArray *arrayModel = [NSArray yy_modelArrayWithClass:[LPZWYMYTestModel class] json:array];
                [weakSelf zhengli:arrayModel];
            }
            
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        [weakSelf.tableView.mj_header endRefreshing];
        
    } failBlock:^(id object) {
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    
}

- (void)zhengli:(NSArray *)array{
    NSMutableArray *noFinsh = [NSMutableArray array];
    NSMutableArray *finsh = [NSMutableArray array];
    for (LPZWYMYTestModel *model  in array) {
        if ([model.graduatestatus integerValue] == 0) {
            [noFinsh addObject:model];
        }else{
            [finsh addObject:model];
        }
    }
    [self.dataArray addObject:@{@"title": @"    未完成考试", @"data": noFinsh}];
     [self.dataArray addObject:@{@"title": @"    已完成考试", @"data": finsh}];
    
     [self.tableView reloadData];
}

////上拉加载
//- (void)netWorking_footer{
//
//    __weak typeof(self) weakSelf = self;
//     [LPRequestManager request_getIfFinishClassInfoUseWeicatAppWithsuccessBlock:^(id object) {
//        NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
//        if ([result integerValue] == 1) {
//            NSArray *array = [[object objectForKey:@"data"] objectForKey:@"dataList"];
//            if (array.count == 0) {
//                _page --;
//            }else{
//                [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPZWYMYTestModel class] json:array]] ;
//                [weakSelf.tableView reloadData];
//            }
//
//        }
//
//        [weakSelf.tableView.mj_footer endRefreshing];
//    } failBlock:^(id object) {
//        [weakSelf.tableView.mj_footer endRefreshing];
//    }];
//
//
//
//}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.navigView , 0)
    .bottomSpaceToView(self.view , 0);
}


#pragma mark - 私有方法


#pragma mark - 监听事件
- (void)LPZWYMYTestTableViewCell_click_section:(NSInteger)section row:(NSInteger)row{
    NSLog(@"你好");
}

- (void)LPZWYMyTestFinshTableViewCell_click_section:(NSInteger)section row:(NSInteger)row{
    NSLog(@"你好");
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [self.dataArray[section] objectForKey:@"data"];
    return array.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 95 * [UIScreen zj_screenHScale];
    }else{
         return 70 * [UIScreen zj_screenHScale];
    }
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array = [self.dataArray[indexPath.section] objectForKey:@"data"];
    
    if (indexPath.section == 0) {
        LPZWYMYTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
        cell.model = array[indexPath.row];
        cell.delegate = self;
    
        return cell;
    }else{
        LPZWYMyTestFinshTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:finshId forIndexPath:indexPath];
        cell.model = array[indexPath.row];
        cell.delegate = self;
        return cell;
    }
   
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * [UIScreen zj_screenHScale];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 40 * [UIScreen zj_screenHScale])];
    header.backgroundColor = [UIColor zj_colorWithHexString:@"f2f2f2"];
    header.font = LYPFONT(15);
    header.textColor = [UIColor textGrayColor];
    NSString *title = [self.dataArray[section] objectForKey:@"title"];
    header.text = title;
    
    return header;
}


- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    
    if (scrollView ==_tableView)
        
    {
        ///////////////////直接写里边的部分
        CGFloat sectionHeaderHeight =40 * [UIScreen zj_screenHScale];//sectionHeaderHeight
        
        if (scrollView.contentOffset.y<= sectionHeaderHeight && scrollView.contentOffset.y>=0) {
            
            scrollView.contentInset=UIEdgeInsetsMake(-scrollView.contentOffset.y,0,0,0);
            
        } else if (scrollView.contentOffset.y>= sectionHeaderHeight) {
            
            scrollView.contentInset=UIEdgeInsetsMake(-sectionHeaderHeight,0,0,0);
            
        }
    }
    
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array = [self.dataArray[indexPath.section] objectForKey:@"data"];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
        [_tableView registerClass:[LPZWYMYTestTableViewCell class] forCellReuseIdentifier:cellId];
        
         [_tableView registerClass:[LPZWYMyTestFinshTableViewCell class] forCellReuseIdentifier:finshId];
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
